﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using YHWE.Web.Models;
using JULONG.WEB.Library;
using JULONG.WEB.Library.expViewResult;
namespace YHWE.Web.Areas.manage.Controllers
{
    public class guestBookController : Controller
    {
        DataSession<entityDB2Container, guestBook> db = new DataSession<entityDB2Container, guestBook>();

        //
        // GET: /manage/guestBook/
        public ActionResult Index()
        {
            ViewData.Model = db.query().OrderByDescending(d => d.ID);
            return View();
        }

        //
        // GET: /manage/guestBook/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData.Model = db.get(d => d.ID == id);
            return View();
        }

        //
        // POST: /manage/guestBook/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var entity = db.get(d => d.ID == id);

            try
            {
                UpdateModel(entity);
                if (entity.replyDatetime == null) 
                    entity.replyDatetime = DateTime.Now;
                db.save();
                return RedirectToAction("Index");
            }
            catch
            {
                ViewData.Model = entity;
                return View();
            }
        }

        //
        // GET: /manage/guestBook/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                db.delete(d => d.ID == id);

                return myJson.success(id);
            }
            catch
            {
                return myJson.error(id);
            }
        }

        //
        // POST: /manage/guestBook/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
